/*  JEVEMon, A Java-based EVEMon clone.
Copyright (C) 2011, Raul Ruiz

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package eveapi;

import java.net.URLEncoder;
import java.net.URL;
import java.net.URLConnection;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author ruizr
 */
public class EVEApiConnector {
    /**
     * 
     */
    private static final String APIBASE = "https://api.eveonline.com";
    //private static final String APIBASE = "http://apitest.eveonline.com";
    
    /**
     * 
     */
    private static final String CHARACTERSURL = APIBASE + "/account/Characters.xml.aspx";
    
    /**
     * 
     */
    private static final String CHARACTERSHEET = APIBASE + "/char/CharacterSheet.xml.aspx";
    
    /**
     * 
     */
    private static final String SERVERSTATUS = APIBASE + "/server/ServerStatus.xml.aspx";
    
    /**
     * 
     */    
    private static final String ACCOUNTSTATUS = APIBASE + "/account/AccountStatus.xml.aspx";
    
    /**
     * 
     */
    private static final String CHARACTERSKILLINTRAINING = APIBASE + "/char/SkillInTraining.xml.aspx";
    
    /**
     * 
     * @param userID
     * @param apiKey
     * @param characterID
     * @return 
     */
    public static String getCharacterSheetXML(String userID, String apiKey, String characterID) {
        String xml_response = "";
        try {
            
            // Construct data
            String data = URLEncoder.encode("userID", "UTF-8") + "=" + URLEncoder.encode(userID, "UTF-8");
            data += "&" + URLEncoder.encode("apiKey", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8");
            data += "&" + URLEncoder.encode("characterID", "UTF-8") + "=" + URLEncoder.encode(characterID, "UTF-8");

            // Send data
            URL url = new URL(CHARACTERSHEET);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                xml_response += line;
            }
            wr.close();
            rd.close();
            
        } 
        catch (Exception e) {}
        return xml_response;
    }
    
    public static String getCharacterSkillInTraining(String userID, String apiKey, String characterID) {
        String xml_response = "";
        try {
            
            // Construct data
            String data = URLEncoder.encode("userID", "UTF-8") + "=" + URLEncoder.encode(userID, "UTF-8");
            data += "&" + URLEncoder.encode("apiKey", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8");
            data += "&" + URLEncoder.encode("characterID", "UTF-8") + "=" + URLEncoder.encode(characterID, "UTF-8");

            // Send data
            URL url = new URL(CHARACTERSKILLINTRAINING);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                xml_response += line;
            }
            wr.close();
            rd.close();
            
        } 
        catch (Exception e) {}
        return xml_response;
    }
    
    /**
     * 
     * @param userID
     * @param apiKey
     * @return 
     */
    public static String getCharacterListXML(String userID, String apiKey) {
        String xml_response = "";
        try {
            
            // Construct data
            String data = URLEncoder.encode("userID", "UTF-8") + "=" + URLEncoder.encode(userID, "UTF-8");
            data += "&" + URLEncoder.encode("apiKey", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8");

            // Send data
            URL url = new URL(CHARACTERSURL);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                xml_response += line;
            }
            wr.close();
            rd.close();
            
        } 
        catch (Exception e) {}
        return xml_response;
    }
    
    /**
     * 
     * @return 
     */
    public static String getServerStatusXML() {
        String xml_response = "";
        try {
            
            // Send data
            URL url = new URL(SERVERSTATUS);
            URLConnection conn = url.openConnection();
            
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                xml_response += line;
            }
            rd.close();
            
        } 
        catch (Exception e) {xml_response = "error";}
        return xml_response;
    }
    
    public static String getAccountStatus(String userID, String apiKey) {
        String xml_response = "";
        try {
            
            // Construct data
            String data = URLEncoder.encode("userID", "UTF-8") + "=" + URLEncoder.encode(userID, "UTF-8");
            data += "&" + URLEncoder.encode("apiKey", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8");

            // Send data
            URL url = new URL(ACCOUNTSTATUS);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                xml_response += line;
            }
            wr.close();
            rd.close();
            
        } 
        catch (Exception e) {}
        return xml_response;
    }

}
